import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class MyTomcat {
    //一个超级简易的自制Tomcat,可以看下面我自己写的，也能大致知道是干嘛的
    //大概来说Tomcat相当于QQmini里面的服务器，接收客户端发送的消息然后转发给浏览器
    //浏览器连接到服务端口
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);

        while (!serverSocket.isClosed()) {
            System.out.println("Waiting for a client ...");
            Socket socket = serverSocket.accept();

            //输入流
            BufferedReader bufferedReader = new BufferedReader(new FileReader("src/hello.html"));
            //输出流
            OutputStream outputStream = socket.getOutputStream();
            String buf = "";
            String responseHeader = "HTTP/1.1 200 OK\r\n" +
                    "Content-Type: text/html; charset=UTF-8\r\n" +
                    "\r\n";
            outputStream.write(responseHeader.getBytes());
            while ((buf = bufferedReader.readLine()) != null) {
                outputStream.write(buf.getBytes());
            }
            outputStream.close();
            socket.close();
        }
        serverSocket.close();
    }
}
